package com.woniuxy.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.response.ResponseResult;
import com.woniuxy.system.demol.entity.Role;
import com.woniuxy.system.demol.entity.RolePerms;
import com.woniuxy.system.demol.entity.User;
import com.woniuxy.system.demol.vo.RoleADPermsVo;
import com.woniuxy.system.demol.vo.RoleAllVo;
import com.woniuxy.system.demol.vo.RoleUpdateVo;
import com.woniuxy.system.demol.vo.RoleVo;
import com.woniuxy.system.service.RolePermsService;
import com.woniuxy.system.service.RoleService;

import com.woniuxy.util.CheckTokenUtil;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author why
 * @since 2023-09-08
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;
    @Resource
    private RolePermsService rolePermsService;

    //查找正在使用所有的角色信息
    @GetMapping("/findOpenRole")
    public ResponseResult<List<Role>> findOpenRole() {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("state", "0");

        List<Role> users = roleService.list(queryWrapper);
        return new ResponseResult<>(ResponseResult.SUCCESS, "success", users);

    }

    //查找所有的角色信息
    @GetMapping("/findAllRole/{currentPage}/{pageSize}")
    public ResponseResult<Map<String, Object>> findAllRole(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize) {
        int offset = 0;
        if (currentPage == 1) {

        } else {
            offset = (currentPage - 1) * pageSize;
        }

        Map<String, Object> all = roleService.findAllRole(offset, pageSize);

        return new ResponseResult<>(ResponseResult.SUCCESS, "success", all);

    }

    //查找所有自定义的角色信息
    @GetMapping("/findAllRole2/{currentPage}/{pageSize}")
    public ResponseResult<Map<String, Object>> findAllRole2(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize) {
        int offset = 0;
        if (currentPage == 1) {

        } else {
            offset = (currentPage - 1) * pageSize;
        }

        Map<String, Object> all = roleService.findAllRole2(offset, pageSize);

        System.out.println(all);
        return new ResponseResult<>(ResponseResult.SUCCESS, "success", all);

    }
    //查找所有自定义的角色信息
    @GetMapping("/findAllRole3/{currentPage}/{pageSize}")
    public ResponseResult<Map<String, Object>> findAllRole3(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize, HttpServletRequest request) {
        String aid = request.getHeader("access_token");
        Map<String, Object> cUser = CheckTokenUtil.getUser(aid);
        Integer i = (Integer) cUser.get("uid");
        int offset = 0;
        if (currentPage == 1) {

        } else {
            offset = (currentPage - 1) * pageSize;
        }

        Map<String, Object> all = roleService.findAllRole3(offset, pageSize,i);

        return new ResponseResult<>(ResponseResult.SUCCESS, "success", all);

    }


    //新增角色信息
    @PreAuthorize("hasAnyAuthority('sale:agreePack','depot:agreeIn','depot:agreeOut','buy:agreeBuy','top:adduser','com:openClient','goods:salePrice','finance:lockaccout')")
    @PostMapping("/addRole")
    public ResponseResult<Boolean> addRole(@RequestBody RoleUpdateVo role, HttpServletRequest request) {
        String aid = request.getHeader("access_token");
        Map<String, Object> cUser = CheckTokenUtil.getUser(aid);
        Integer i = (Integer) cUser.get("uid");
        //将对象的aid赋值
        role.setAid(i);
        Boolean add = roleService.addRole(role);
        if (add) {
            return new ResponseResult<>(ResponseResult.SUCCESS, "success", add);
        }

        return new ResponseResult<>(ResponseResult.SYSTEM_ERROR, "error", null);

    }

    //修改角色基本信息
    @PreAuthorize("hasAnyAuthority('sale:agreePack','depot:agreeIn','depot:agreeOut','buy:agreeBuy','top:adduser','com:openClient','goods:salePrice','finance:lockaccout')")
    @PostMapping("/updateRole")
    public ResponseResult<Boolean> updateRole(@RequestBody RoleUpdateVo role, HttpServletRequest request) {
        String aid = request.getHeader("access_token");
        Map<String, Object> cUser = CheckTokenUtil.getUser(aid);
        Integer i = (Integer) cUser.get("uid");
        //将对象的aid赋值
        role.setAid(i);
        Boolean add = roleService.updateRole(role);
        if (add) {
            return new ResponseResult<>(ResponseResult.SUCCESS, "success", add);
        }

        return new ResponseResult<>(ResponseResult.SYSTEM_ERROR, "error", null);

    }


    //修改角色对应的权限
    @PreAuthorize("hasAnyAuthority('sale:agreePack','depot:agreeIn','depot:agreeOut','buy:agreeBuy','top:adduser','com:openClient','goods:salePrice','finance:lockaccout')")
    @PostMapping("/addDelPerms")
    public ResponseResult<Boolean> addDelPerms(@RequestBody RoleADPermsVo roleADPermsVo, ServletRequest servletRequest) {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String aid = request.getHeader("uid");
        int i = Integer.valueOf(aid).intValue();
        roleADPermsVo.setAid(i);
        Boolean update = roleService.addDelPerms(roleADPermsVo);

        return new ResponseResult<>(ResponseResult.SUCCESS, "success", update);
    }


    //删除角色
    @PreAuthorize("hasAnyAuthority('sale:agreePack','depot:agreeIn','depot:agreeOut','buy:agreeBuy','top:adduser','com:openClient','goods:salePrice','finance:lockaccout')")
    @GetMapping("/delRole/{uid}")
    public ResponseResult<Boolean> delRole(@PathVariable("uid") Integer rid, HttpServletRequest request) {
        String aid = request.getHeader("access_token");
        Map<String, Object> cUser = CheckTokenUtil.getUser(aid);
        Integer i = (Integer) cUser.get("uid");
        Role role = new Role().setAid(i).setState(3).setId(rid);
        boolean b = roleService.saveOrUpdate(role);
        if (b) {
            Boolean a = roleService.delRole(i, rid);
        }
        return new ResponseResult<>(ResponseResult.SUCCESS, "success", b);
    }

    //查看所有启用的
    @GetMapping("/findStateOpen/{currentPage}/{pageSize}")
    public ResponseResult<Map<String, Object>> findStateOpen(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize) {
        int i = roleService.pageListCount();
        int offset = 0;
        if (currentPage == 1) {
        } else {
            offset = (currentPage - 1) * pageSize;
        }
        Map<String, Object> all2 = roleService.findStateOpen(offset, pageSize);
        // System.out.println(all2);
        return new ResponseResult<>(ResponseResult.SUCCESS, "success", all2);
    }

    //查看所有禁用的
    @GetMapping("/findStateClose/{currentPage}/{pageSize}")
    public ResponseResult<Map<String, Object>> findStateClose(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize) {
        int i = roleService.pageListCount();
        int offset = 0;
        if (currentPage == 1) {
        } else {
            offset = (currentPage - 1) * pageSize;
        }
        Map<String, Object> all2 = roleService.findStateClose(offset, pageSize);
        return new ResponseResult<>(ResponseResult.SUCCESS, "success", all2);
    }

    //多条件搜索
    @GetMapping("/searchAll/{num}/{input}/{currentPage}/{pageSize}")
    public ResponseResult<Map<String, Object>> findStateClose(@PathVariable("num") Integer num, @PathVariable("input") String input, @PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize) {
        int i = roleService.pageListCount();
        int offset = 0;
        if (currentPage == 1) {
        } else {
            offset = (currentPage - 1) * pageSize;
        }
        Map<String, Object> all2 = roleService.searchAll(num, input, offset, pageSize);
        return new ResponseResult<>(ResponseResult.SUCCESS, "success", all2);
    }

    //停用角色
    @PreAuthorize("hasAnyAuthority('sale:agreePack','depot:agreeIn','depot:agreeOut','buy:agreeBuy','top:adduser','com:openClient','goods:salePrice','finance:lockaccout')")
    @GetMapping("/stopRole/{id}")
    public ResponseResult<Boolean> stopRole(@PathVariable("id") Integer id, HttpServletRequest request) {
        System.out.println(id);
        String aid = request.getHeader("access_token");
        Map<String, Object> cUser = CheckTokenUtil.getUser(aid);
        Integer i = (Integer) cUser.get("uid");
        Role role = new Role().setAid(i).setState(1).setId(id);
        boolean b = roleService.saveOrUpdate(role);
        Role byId = roleService.getById(id);
        String name = byId.getName();
        boolean c = roleService.updateRoleState(1, name,id);
        if (c){
            return new ResponseResult<>(ResponseResult.SUCCESS, "success", c);
        }
        return new ResponseResult<>(ResponseResult.SYSTEM_ERROR, "error", c);
    }

    //启用角色
    @PreAuthorize("hasAnyAuthority('sale:agreePack','depot:agreeIn','depot:agreeOut','buy:agreeBuy','top:adduser','com:openClient','goods:salePrice','finance:lockaccout')")
    @GetMapping("/openRole/{id}")
    public ResponseResult<Boolean> openRole(@PathVariable("id") Integer id, HttpServletRequest request) {
        String aid = request.getHeader("access_token");
        Map<String, Object> cUser = CheckTokenUtil.getUser(aid);
        Integer i = (Integer) cUser.get("uid");
        Role role = new Role().setAid(i).setState(0).setId(id);
        boolean b = roleService.saveOrUpdate(role);
        Role byId = roleService.getById(id);
        String name = byId.getName();
        boolean c = roleService.updateRoleState(0, name,id);
        return new ResponseResult<>(ResponseResult.SUCCESS, "success", b);
    }

    @GetMapping("/findRoleByAccount/{role}")
    public ResponseResult<RoleAllVo> findRoleByAccount(@PathVariable("role") String account) {
        RoleAllVo roleAllVo = roleService.findRoleByAccount(account);
        return new ResponseResult<>(ResponseResult.SUCCESS, "success", roleAllVo);
    }

}

